import logging
from database import DatabaseManager

# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

class GiftLedger:
    def __init__(self, ledger_type):
        self.ledger_type = ledger_type
        self.db = DatabaseManager('gift_ledger.db')
        self.records = self.db.get_records(ledger_type)

    def add_record(self, data):
        record_id = self.db.add_record(self.ledger_type, data)
        if record_id:
            data['id'] = record_id
            self.records = self.db.get_records(self.ledger_type)
        return record_id

    def delete_record(self, record_id):
        self.db.delete_record(self.ledger_type, record_id)
        self.records = self.db.get_records(self.ledger_type)

    def update_record(self, record_id, new_data):
        if self.db.update_record(self.ledger_type, record_id, new_data):
            self.records = self.db.get_records(self.ledger_type)
            return True
        return False

    def get_records(self):
        return self.records